﻿using System;
using System.IO;
using System.Linq;

namespace Lbb.IO
{
    /// <summary>
    /// 文件操作助理。
    /// </summary>
    /// <author>Chiron</author>
    public class FileHelper
    {
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// Author      Chiron
        public static void DeleteFile(string filename)
        {
            var fileInfo = new FileInfo(filename);
            if (fileInfo.Exists)
            {
                if (fileInfo.IsReadOnly) fileInfo.IsReadOnly = false;
                fileInfo.Delete();
            }
        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="path">文件夹路径</param>
        /// <param name="recursive">若为 true，则删除此目录、其子目录以及所有文件；否则为 false。</param>
        /// Author      Chiron
        public static void DeleteDirectory(string path, bool recursive = true)
        {
            var dir = new DirectoryInfo(path);
            if (dir.Exists)
            {
                foreach (var file in dir.GetFiles("*", SearchOption.AllDirectories).Where(d => d.IsReadOnly))
                {
                    file.IsReadOnly = false;
                }
                var dirs = dir.GetDirectories("*", SearchOption.AllDirectories).OrderByDescending(d => d.FullName);
                foreach (var d in dirs)
                {
                    d.Delete(recursive);
                    System.Threading.Thread.Sleep(100);
                }
                dir.Delete(recursive);

            }
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="dir">源文件夹</param>
        /// <param name="targetPath">目标文件夹</param>
        /// <param name="copySubDir">是否复制子文件夹</param>
        /// <author>Chiron</author>
        public static void CopyDirectory(DirectoryInfo dir, string targetPath, bool copySubDir = true)
        {
            if (dir == null) throw new ArgumentNullException("dir");
            if (string.IsNullOrWhiteSpace(targetPath)) throw new ArgumentNullException("targetPath");

            if (!dir.Exists) return;

            if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath);

            foreach (var file in dir.GetFiles())
            {
                var tagFileName = Path.Combine(targetPath, file.Name);
                var tagFile = new FileInfo(tagFileName);
                if (tagFile.Exists && tagFile.IsReadOnly) tagFile.IsReadOnly = false;
                file.CopyTo(tagFileName, true);
            }
            if (copySubDir)
            {
                foreach (var subDir in dir.GetDirectories())
                {
                    CopyDirectory(subDir, Path.Combine(targetPath, subDir.Name), copySubDir);
                }
            }
        }

        /// <summary>
        /// 转空间大小为字符串。
        /// </summary>
        /// <param name="size">空间大小</param>
        /// <returns></returns>
        public static string ToFriendlySize(long size)
        {
            bool isNegative = size < 0;
            size = Math.Abs(size);
            string s = size + " bytes";
            long kb = 1024;
            long mb = kb * 1024;
            long gb = mb * 1024;
            if (size > kb && size < mb)
            {
                s = (size / kb) + " KB";
            }
            else if (size >= mb && size < gb)
            {
                s = (size / mb) + " MB";
            }
            else if (size > gb)
            {
                s = (size / gb) + " GB";
            }
            if (isNegative)
            {
                s = "-" + s;
            }
            return s;
        }
    }
}
